﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Data;
using Model;
using Utils;

namespace Website.uc
{
    public partial class ucCategory : System.Web.UI.UserControl
    {
        CategoryDao cateDao = new CategoryDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack){
                loadCate();
            }
        }

        private void loadCate()
        {
            DataTable tb = cateDao.getAllCategory();
            GridView1.DataSource = tb;
            GridView1.DataBind();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Category cate = new Category();
            try
            {

                if (CheckValidate.CheckNull(txtCateName.Text))
                {
                    lblMessage.Text = "Enter category name !!";
                    txtCateName.Focus();
                }
                else if (CheckValidate.CheckNull(txtCateStatus.Text))
                {
                    lblMessage.Text = "Enter category status !!";
                    txtCateStatus.Focus();
                }
                else
                {
                    cate.CateName = txtCateName.Text;
                    cate.CateStatus = txtCateStatus.Text;
                    if (lblId.Text.Length > 0)
                    {
                        cate.CategoryId = Int32.Parse(lblId.Text);
                        cateDao.updateCategory(cate);
                    }
                    else
                    {
                        bool flag = cateDao.insertCategory(cate);
                    }
                    loadCate();
                    reset();
                }
            }
            catch(Exception ex){
                Utility.ShowMessage(ex.ToString() , Page);
            }


        }

        private void reset()
        {
            lblId.Text = "";
            lblMessage.Text = "";
            txtCateName.Text = "";
            txtCateStatus.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = Utility.GetValueGridView(GridView1 , 2 , e);
            lblId.Text = id;
            DataTable tb = cateDao.getCategoryById(id);
            txtCateName.Text = tb.Rows[0]["CateName"].ToString();
            txtCateStatus.Text = tb.Rows[0]["CateStatus"].ToString();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string id = Utility.GetDataKey( GridView1 , e );
                cateDao.deleteCategory(id);
                loadCate();
                reset();
                Utility.ShowMessage("Delete successfull !!", Page);
            }
            catch(Exception ex){
                Utility.ShowMessage("Can not delete this category !!", Page);
            }
        }
    }
}